/**
 * 详情页面
 */
var shopFileinputDefaults = {
    language: 'zh', //设置语言
    uploadUrl: ctx + "common/sysFile/upload", //上传的地址
    deleteUrl: ctx + "common/sysFile/remove", //删除的地址
    allowedFileTypes : ['image'],
    //allowedFileExtensions: ['jpg'],//接收的文件后缀
    uploadAsync: true, //默认异步上传
    showUpload: false, //是否显示上传按钮
    showRemove : false, //显示移除按钮
    showPreview : true, //是否显示预览
    showDownload: false, //是否显示下载
    showCaption: false,//是否显示标题
    allowedPreviewTypes : [ 'image' ],
    initialPreviewAsData: true,
    initialPreviewFileType: 'image',
    dropZoneTitle: "请通过拖拽图片文件放到这里",
    dropZoneClickTitle: "或者点击此区域添加图片",
    browseClass: "btn btn-primary", //按钮样式
    removeClass: 'btn btn-default',
    cancelClass: 'btn btn-default',
    uploadClass: 'btn btn-default',
    dropZoneEnabled: true,//是否显示拖拽区域
    maxFileCount: 1, //表示允许同时上传的最大文件个数
    enctype: 'multipart/form-data',
    validateInitialCount: true,
    minImageWidth: 50,
    minImageHeight: 50,
    previewSettings: {
        image: {
            width: "auto",
            height: "140px"
        }
    }
};

$(function() {
    layui.use(['form', 'layedit', 'laydate','table',], function(){
        var form = layui.form;
        //监听指定开关
        form.on('switch(switchTest)', function(data){
            console.log('data',data);
            console.log('this.checked',this.checked);
            var type = data.value;
            var isCheck = 0;
            if(this.checked == true){
                isCheck = 1
            }else{
                isCheck = 0
            }
            changeShopType(type,isCheck)
        });

    });

    if (!$.isEmptyObject(logoUrlFile)) {
        var initialLogoPreview = [], initialLogoPreviewConfig = [];
        $(logoUrlFile).each(function(i, file) {
            initialLogoPreview.push(file.url);
            initialLogoPreviewConfig.push({
                url : ctx + 'common/sysFile/remove',
                extra : {
                    id : file.id
                }
            });
        });
        var initialLogoConf = {
            //不覆盖已存在的图片
            overwriteInitial: true,
            initialPreviewAsData: true,
            initialPreviewFileType: 'image',
            //要显示的图片的路径
            initialPreview: initialLogoPreview,
            initialPreviewConfig: initialLogoPreviewConfig
        }
    }

    $("#uploadfile").fileinput($.extend(true, {}, shopFileinputDefaults, initialLogoConf || {}, {
        uploadExtraData : function () {
            return {
                associatedId : 'shop_logo$' + $('#signupForm').find("#id").val(),
                id:$('#signupForm').find("#id").val(),
                imgType: "shop_logo",
                isSingle: 1,
                upload_type: 1
            };
        }
    })).on("filebatchselected", function(e, files) {
        $(this).fileinput("upload");             // 文件选择完直接调用上传方法。
    }).on('fileuploaded', function (e, data, previewId, index) {
        $('#shopLogo').val(data.response.filePath);
    });

    $('#uploadfile').on('filesuccessremove', function(event, id) {
        if (id) {
            console.log('Uploaded thumbnail successfully removed');
        } else {
            parent.layer.alert("删除文件错误！请再尝试！");
            return false;
        }
    });
});


function changeShopType(type,isCheck){
    var shopTypes = $("#hidShopTypes").val();
    var typeJson = JSON.parse(shopTypes);
    for (var index in typeJson){
        var typeValue = typeJson[index].typeValue;
        if(typeValue == type){
            typeJson[index].isCheck = isCheck;
        }
    }
    var typeStr = JSON.stringify(typeJson);
    $("#hidShopTypes").val(typeStr);
}


$().ready(function() {
    validateRule();
});

$.validator.setDefaults({
    submitHandler : function() {
        update();
    }
});


function update() {

    var url = ctx +"module/shop/" + ($('#signupForm').find("#id").val() == '' ? "save" : "update");
    $.ajax({
        cache : true,
        type : "POST",
        url : url,
        data : $('#signupForm').serialize(),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }
        }
    });
}


function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules : {
            sort : {
                required : true
            }
        },
        messages : {
            sort : {
                required : icon + "请输入索引"
            }
        }
    })
}
